package com.gulimall.product.web;

import com.gulimall.product.entity.CategoryEntity;
import com.gulimall.product.service.CategoryService;
import com.gulimall.product.vo.categoryvo.Category2Vo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @Author by董江
 * @Tool Intellij IDEA
 * @Classname IndexController
 * @Date 2022/5/11 23:43
 */
@Controller
public class IndexController {
    @Autowired
    private CategoryService categoryService;

    @GetMapping({"/", "/index.html"})
    public String indexPage(Model model) {
        //查到所有的一级分类
        List<CategoryEntity> levelOneCategorys = categoryService.getLevelOneCategorys();

        model.addAttribute("categorys", levelOneCategorys);

        return "index";
    }

    @ResponseBody
    @GetMapping("/index/catalog.json")
    public Map<Long , List<Category2Vo>> getCatalogJson() {
        return categoryService.getCatalogJson();
    }
}
